package org.hy.util;

import static org.hy.util.XMLUtil.persisDataToXml;

import org.hy.domain.group.GDiscuss;
import org.hy.domain.group.GMember;
import org.hy.domain.group.Group;
import org.hy.persistence.BaseSpringTest;
import org.hy.service.group.IGDiscussService;
import org.hy.service.group.IGMemberService;
import org.hy.service.group.IGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.testng.annotations.Test;

/**
 *  产生测试需要的数据
 * @author Administrator
 *
 */
public class XMLDataGenerateDemo extends BaseSpringTest{

	//注入服务类,用于从数据库获取源数据.
	@Autowired private IGroupService groupService ;
	@Autowired private IGMemberService gMemberService;
	@Autowired private IGDiscussService gDiscussService;
	
	//xml示例数据的根目录
	private static final String DIR_PREFIX = "test/main/xmldata/";
	
	@Test
	public void forGroup() {
		int id = 1;
		String dir = getDirForClass(Group.class);
		Group group = groupService.findGroupById(id);
		persisDataToXml(group, dir);
	}
	
	@Test
	public void forGMember() {
		int id = 1;
		String dir = getDirForClass(GMember.class);
		GMember gmember = gMemberService.findGMemberById(id);
		persisDataToXml(gmember, dir);
	}
	
	@Test
	public void forGDiscuss() {
		int id = 1;
		String dir = getDirForClass(GDiscuss.class);
		GDiscuss discussWithComments = gDiscussService.findGDiscussWithGAnswerById(id, 1, AppConstant.PAGE_SIZE);
		persisDataToXml(discussWithComments, dir);
	}
	
	/**
	 * 获取指定类对应的测试目录,以类名的小写形式作为目录名称
	 * @param clazz  指定类
	 * @return 测试数据存放的目录
	 */
	private String getDirForClass(Class<?> clazz){
		String fullName = clazz.getName();
		String shortName = fullName;
		int dotIndex = fullName.lastIndexOf(".");
		if(dotIndex != -1){
			shortName = fullName.substring(dotIndex+1);
		}else{
			throw new RuntimeException("不支持的类型"+shortName);
		}
		return DIR_PREFIX+shortName.toLowerCase();
	}

}
